home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / automake-1.8 / Automake / Rule.pm < prev    next >
Encoding:
Perl POD Document  |  2005-10-16  |  22.1 KB  |  839 lines

  1. # Copyright (C) 2003  Free Software Foundation, Inc.
  2.  
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7.  
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12.  
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  16. # 02111-1307, USA.
  17.  
  18. package Automake::Rule;
  19. use strict;
  20. use Carp;
  21.  
  22. use Automake::Item;
  23. use Automake::RuleDef;
  24. use Automake::ChannelDefs;
  25. use Automake::Channels;
  26. use Automake::Options;
  27. use Automake::Condition qw (TRUE FALSE);
  28. use Automake::DisjConditions;
  29. require Exporter;
  30. use vars '@ISA', '@EXPORT', '@EXPORT_OK';
  31. @ISA = qw/Automake::Item Exporter/;
  32. @EXPORT = qw (reset register_suffix_rule suffix_rules_count
  33.           suffixes rules $suffix_rules $KNOWN_EXTENSIONS_PATTERN
  34.           depend %dependencies %actions accept_extensions
  35.           reject_rule msg_rule msg_cond_rule err_rule err_cond_rule
  36.           rule rrule ruledef rruledef);
  37.  
  38. =head1 NAME
  39.  
  40. Automake::Rule - support for rules definitions
  41.  
  42. =head1 SYNOPSIS
  43.  
  44.   use Automake::Rule;
  45.   use Automake::RuleDef;
  46.  
  47.  
  48. =head1 DESCRIPTION
  49.  
  50. This package provides support for Makefile rule definitions.
  51.  
  52. An C<Automake::Rule> is a rule name associated to possibly
  53. many conditional definitions.  These definitions are instances
  54. of C<Automake::RuleDef>.
  55.  
  56. Therefore obtaining the value of a rule under a given
  57. condition involves two lookups.  One to look up the rule,
  58. and one to look up the conditional definition:
  59.  
  60.   my $rule = rule $name;
  61.   if ($rule)
  62.     {
  63.       my $def = $rule->def ($cond);
  64.       if ($def)
  65.         {
  66.           return $def->location;
  67.         }
  68.       ...
  69.     }
  70.   ...
  71.  
  72. when it is known that the rule and the definition
  73. being looked up exist, the above can be simplified to
  74.  
  75.   return rule ($name)->def ($cond)->location; # do not write this.
  76.  
  77. but is better written
  78.  
  79.   return rrule ($name)->rrule ($cond)->location;
  80.  
  81. or even
  82.  
  83.   return rruledef ($name, $cond)->location;
  84.  
  85. The I<r> variants of the C<rule>, C<def>, and C<ruledef> methods add
  86. an extra test to ensure that the lookup succeeded, and will diagnose
  87. failures as internal errors (with a message which is much more
  88. informative than Perl's warning about calling a method on a
  89. non-object).
  90.  
  91. =head2 Global variables
  92.  
  93. =over 4
  94.  
  95. =cut
  96.  
  97. my $_SUFFIX_RULE_PATTERN =
  98.   '^(\.[a-zA-Z0-9_(){}$+@\-]+)(\.[a-zA-Z0-9_(){}$+@\-]+)' . "\$";
  99.  
  100. # Suffixes found during a run.
  101. use vars '@_suffixes';
  102.  
  103. # Same as $suffix_rules (declared below), but records only the
  104. # default rules supplied by the languages Automake supports.
  105. use vars '$_suffix_rules_default';
  106.  
  107. =item C<%dependencies>
  108.  
  109. Holds the dependencies of targets which dependencies are factored.
  110. Typically, C<.PHONY> will appear in plenty of F<*.am> files, but must
  111. be output once.  Arguably all pure dependencies could be subject to
  112. this factorization, but it is not unpleasant to have paragraphs in
  113. Makefile: keeping related stuff altogether.
  114.  
  115. =cut
  116.  
  117. use vars '%dependencies';
  118.  
  119. =item <%actions>
  120.  
  121. Holds the factored actions.  Tied to C<%dependencies>, i.e., filled
  122. only when keys exists in C<%dependencies>.
  123.  
  124. =cut
  125.  
  126. use vars '%actions';
  127.  
  128. =item <$suffix_rules>
  129.  
  130. This maps the source extension for all suffix rule seen to
  131. a C<hash> whose keys are the possible output extensions.
  132.  
  133. Note that this is transitively closed by construction:
  134. if we have
  135.       exists $suffix_rules{$ext1}{$ext2}
  136.    && exists $suffix_rules{$ext2}{$ext3}
  137. then we also have
  138.       exists $suffix_rules{$ext1}{$ext3}
  139.  
  140. So it's easy to check whether C<.foo> can be transformed to
  141. C<.$(OBJEXT)> by checking whether
  142. C<$suffix_rules{'.foo'}{'.$(OBJEXT)'}> exists.  This will work even if
  143. transforming C<.foo> to C<.$(OBJEXT)> involves a chain of several
  144. suffix rules.
  145.  
  146. The value of C<$suffix_rules{$ext1}{$ext2}> is the a pair
  147. C<[ $next_sfx, $dist ]> where C<$next_sfx> is target suffix
  148. for the next rule to use to reach C<$ext2>, and C<$dist> the
  149. distance to C<$ext2'>.
  150.  
  151. The content of this variable should be updated via the
  152. C<register_suffix_rule> function.
  153.  
  154. =cut
  155.  
  156. use vars '$suffix_rules';
  157.  
  158. =item C<$KNOWN_EXTENSIONS_PATTERN>
  159.  
  160. Pattern that matches all know input extensions (i.e. extensions used
  161. by the languages supported by Automake).  Using this pattern (instead
  162. of `\..*$') to match extensions allows Automake to support dot-less
  163. extensions.
  164.  
  165. New extensions should be registered with C<accept_extensions>.
  166.  
  167. =cut
  168.  
  169. use vars qw ($KNOWN_EXTENSIONS_PATTERN @_known_extensions_list);
  170. $KNOWN_EXTENSIONS_PATTERN = "";
  171. @_known_extensions_list = ();
  172.  
  173. =back
  174.  
  175. =head2 Error reporting functions
  176.  
  177. In these functions, C<$rule> can be either a rule name, or
  178. an instance of C<Automake::Rule>.
  179.  
  180. =over 4
  181.  
  182. =item C<err_rule ($rule, $message, [%options])>
  183.  
  184. Uncategorized errors about rules.
  185.  
  186. =cut
  187.  
  188. sub err_rule ($$;%)
  189. {
  190.   msg_rule ('error', @_);
  191. }
  192.  
  193. =item C<err_cond_rule ($cond, $rule, $message, [%options])>
  194.  
  195. Uncategorized errors about conditional rules.
  196.  
  197. =cut
  198.  
  199. sub err_cond_rule ($$$;%)
  200. {
  201.   msg_cond_rule ('error', @_);
  202. }
  203.  
  204. =item C<msg_cond_rule ($channel, $cond, $rule, $message, [%options])>
  205.  
  206. Messages about conditional rules.
  207.  
  208. =cut
  209.  
  210. sub msg_cond_rule ($$$$;%)
  211. {
  212.   my ($channel, $cond, $rule, $msg, %opts) = @_;
  213.   my $r = ref ($rule) ? $rule : rrule ($rule);
  214.   msg $channel, $r->rdef ($cond)->location, $msg, %opts;
  215. }
  216.  
  217. =item C<msg_rule ($channel, $targetname, $message, [%options])>
  218.  
  219. Messages about rules.
  220.  
  221. =cut
  222.  
  223. sub msg_rule ($$$;%)
  224. {
  225.   my ($channel, $rule, $msg, %opts) = @_;
  226.   my $r = ref ($rule) ? $rule : rrule ($rule);
  227.   # Don't know which condition is concerned.  Pick any.
  228.   my $cond = $r->conditions->one_cond;
  229.   msg_cond_rule ($channel, $cond, $r, $msg, %opts);
  230. }
  231.  
  232.  
  233. =item C<$bool = reject_rule ($rule, $error_msg)>
  234.  
  235. Bail out with C<$error_msg> if a rule with name C<$rule> has been
  236. defined.
  237.  
  238. Return true iff C<$rule> is defined.
  239.  
  240. =cut
  241.  
  242. sub reject_rule ($$)
  243. {
  244.   my ($rule, $msg) = @_;
  245.   if (rule ($rule))
  246.     {
  247.       err_rule $rule, $msg;
  248.       return 1;
  249.     }
  250.   return 0;
  251. }
  252.  
  253. =back
  254.  
  255. =head2 Administrative functions
  256.  
  257. =over 4
  258.  
  259. =item C<accept_extensions (@exts)>
  260.  
  261. Update C<$KNOWN_EXTENSIONS_PATTERN> to recognize the extensions
  262. listed C<@exts>.  Extensions should contain a dot if needed.
  263.  
  264. =cut
  265.  
  266. sub accept_extensions (@)
  267. {
  268.     push @_known_extensions_list, @_;
  269.     $KNOWN_EXTENSIONS_PATTERN =
  270.     '(?:' . join ('|', map (quotemeta, @_known_extensions_list)) . ')';
  271. }
  272.  
  273. =item C<rules>
  274.  
  275. Returns the list of all L<Automake::Rule> instances.  (I.e., all
  276. rules defined so far.)
  277.  
  278. =cut
  279.  
  280. use vars '%_rule_dict';
  281. sub rules ()
  282. {
  283.   return values %_rule_dict;
  284. }
  285.  
  286.  
  287. =item C<Automake::Rule::reset>
  288.  
  289. The I<forget all> function.  Clears all know rules and reset some
  290. other internal data.
  291.  
  292. =cut
  293.  
  294. sub reset()
  295. {
  296.   %_rule_dict = ();
  297.   @_suffixes = ();
  298.   # The first time we initialize the variables,
  299.   # we save the value of $suffix_rules.
  300.   if (defined $_suffix_rules_default)
  301.     {
  302.       $suffix_rules = $_suffix_rules_default;
  303.     }
  304.   else
  305.     {
  306.       $_suffix_rules_default = $suffix_rules;
  307.     }
  308.  
  309.   %dependencies =
  310.     (
  311.      # Texinfoing.
  312.      'dvi'      => [],
  313.      'dvi-am'   => [],
  314.      'pdf'      => [],
  315.      'pdf-am'   => [],
  316.      'ps'       => [],
  317.      'ps-am'    => [],
  318.      'info'     => [],
  319.      'info-am'  => [],
  320.      'html'     => [],
  321.      'html-am'  => [],
  322.  
  323.      # Installing/uninstalling.
  324.      'install-data-am'      => [],
  325.      'install-exec-am'      => [],
  326.      'uninstall-am'         => [],
  327.  
  328.      'install-man'        => [],
  329.      'uninstall-man'        => [],
  330.  
  331.      'install-info'         => [],
  332.      'install-info-am'      => [],
  333.      'uninstall-info'       => [],
  334.  
  335.      'installcheck-am'      => [],
  336.  
  337.      # Cleaning.
  338.      'clean-am'             => [],
  339.      'mostlyclean-am'       => [],
  340.      'maintainer-clean-am'  => [],
  341.      'distclean-am'         => [],
  342.      'clean'                => [],
  343.      'mostlyclean'          => [],
  344.      'maintainer-clean'     => [],
  345.      'distclean'            => [],
  346.  
  347.      # Tarballing.
  348.      'dist-all'             => [],
  349.  
  350.      # Phoning.
  351.      '.PHONY'               => [],
  352.      );
  353.   %actions = ();
  354. }
  355.  
  356. =item C<register_suffix_rule ($where, $src, $dest)>
  357.  
  358. Register a suffix rules defined on C<$where> that transform
  359. files ending in C<$src> into files ending in C<$dest>.
  360.  
  361. This upgrades the C<$suffix_rules> variables.
  362.  
  363. =cut
  364.  
  365. sub register_suffix_rule ($$$)
  366. {
  367.   my ($where, $src, $dest) = @_;
  368.  
  369.   verb "Sources ending in $src become $dest";
  370.   push @_suffixes, $src, $dest;
  371.  
  372.   # When transforming sources to objects, Automake uses the
  373.   # %suffix_rules to move from each source extension to
  374.   # `.$(OBJEXT)', not to `.o' or `.obj'.  However some people
  375.   # define suffix rules for `.o' or `.obj', so internally we will
  376.   # consider these extensions equivalent to `.$(OBJEXT)'.  We
  377.   # CANNOT rewrite the target (i.e., automagically replace `.o'
  378.   # and `.obj' by `.$(OBJEXT)' in the output), or warn the user
  379.   # that (s)he'd better use `.$(OBJEXT)', because Automake itself
  380.   # output suffix rules for `.o' or `.obj'...
  381.   $dest = '.$(OBJEXT)' if ($dest eq '.o' || $dest eq '.obj');
  382.  
  383.   # Reading the comments near the declaration of $suffix_rules might
  384.   # help to understand the update of $suffix_rules that follows...
  385.  
  386.   # Register $dest as a possible destination from $src.
  387.   # We might have the create the \hash.
  388.   if (exists $suffix_rules->{$src})
  389.     {
  390.       $suffix_rules->{$src}{$dest} = [ $dest, 1 ];
  391.     }
  392.   else
  393.     {
  394.       $suffix_rules->{$src} = { $dest => [ $dest, 1 ] };
  395.     }
  396.  
  397.   # If we know how to transform $dest in something else, then
  398.   # we know how to transform $src in that "something else".
  399.   if (exists $suffix_rules->{$dest})
  400.     {
  401.       for my $dest2 (keys %{$suffix_rules->{$dest}})
  402.     {
  403.       my $dist = $suffix_rules->{$dest}{$dest2}[1] + 1;
  404.       # Overwrite an existing $src->$dest2 path only if
  405.       # the path via $dest which is shorter.
  406.       if (! exists $suffix_rules->{$src}{$dest2}
  407.           || $suffix_rules->{$src}{$dest2}[1] > $dist)
  408.         {
  409.           $suffix_rules->{$src}{$dest2} = [ $dest, $dist ];
  410.         }
  411.     }
  412.     }
  413.  
  414.   # Similarly, any extension that can be derived into $src
  415.   # can be derived into the same extensions as $src can.
  416.   my @dest2 = keys %{$suffix_rules->{$src}};
  417.   for my $src2 (keys %$suffix_rules)
  418.     {
  419.       if (exists $suffix_rules->{$src2}{$src})
  420.     {
  421.       for my $dest2 (@dest2)
  422.         {
  423.           my $dist = $suffix_rules->{$src}{$dest2} + 1;
  424.           # Overwrite an existing $src2->$dest2 path only if
  425.           # the path via $src is shorter.
  426.           if (! exists $suffix_rules->{$src2}{$dest2}
  427.           || $suffix_rules->{$src2}{$dest2}[1] > $dist)
  428.         {
  429.           $suffix_rules->{$src2}{$dest2} = [ $src, $dist ];
  430.         }
  431.         }
  432.     }
  433.     }
  434. }
  435.  
  436. =item C<$count = suffix_rules_count>
  437.  
  438. Return the number of suffix rules added while processing the current
  439. F<Makefile> (excluding predefined suffix rules).
  440.  
  441. =cut
  442.  
  443. sub suffix_rules_count ()
  444. {
  445.   return (scalar keys %$suffix_rules) - (scalar keys %$_suffix_rules_default);
  446. }
  447.  
  448. =item C<@list = suffixes>
  449.  
  450. Return the list of known suffixes.
  451.  
  452. =cut
  453.  
  454. sub suffixes ()
  455. {
  456.   return @_suffixes;
  457. }
  458.  
  459. =item C<rule ($rulename)>
  460.  
  461. Return the C<Automake::Rule> object for the rule
  462. named C<$rulename> if defined.   Return 0 otherwise.
  463.  
  464. =cut
  465.  
  466. sub rule ($)
  467. {
  468.   my ($name) = @_;
  469.   # Strip $(EXEEXT) from $name, so we can diagnose
  470.   # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
  471.   $name =~ s,\$\(EXEEXT\)$,,;
  472.   return $_rule_dict{$name} if exists $_rule_dict{$name};
  473.   return 0;
  474. }
  475.  
  476. =item C<rule ($rulename, $cond>
  477.  
  478. Return the C<Automake::RuleDef> object for the rule named
  479. C<$rulename> if defined in condition C<$cond>.  Return false
  480. if the condition or the rule does not exist.
  481.  
  482. =cut
  483.  
  484. sub ruledef ($$)
  485. {
  486.   my ($name, $cond) = @_;
  487.   my $rule = rule $name;
  488.   return $rule && $rule->def ($cond);
  489. }
  490.  
  491. =item C<rrule ($rulename)
  492.  
  493. Return the C<Automake::Rule> object for the variable named
  494. C<$rulename>.  Abort with an internal error if the variable was not
  495. defined.
  496.  
  497. The I<r> in front of C<var> stands for I<required>.  One
  498. should call C<rvar> to assert the rule's existence.
  499.  
  500. =cut
  501.  
  502. sub rrule ($)
  503. {
  504.   my ($name) = @_;
  505.   my $r = rule $name;
  506.   prog_error ("undefined rule $name\n" . &rules_dump)
  507.     unless $r;
  508.   return $r;
  509. }
  510.  
  511. =item C<rruledef ($varname, $cond)>
  512.  
  513. Return the C<Automake::RuleDef> object for the rule named
  514. C<$rulename> if defined in condition C<$cond>.  Abort with an internal
  515. error if the condition or the rule does not exist.
  516.  
  517. =cut
  518.  
  519. sub rruledef ($$)
  520. {
  521.   my ($name, $cond) = @_;
  522.   return rrule ($name)->rdef ($cond);
  523. }
  524.  
  525. # Create the variable if it does not exist.
  526. # This is used only by other functions in this package.
  527. sub _crule ($)
  528. {
  529.   my ($name) = @_;
  530.   my $r = rule $name;
  531.   return $r if $r;
  532.   return _new Automake::Rule $name;
  533. }
  534.  
  535. sub _new ($$)
  536. {
  537.   my ($class, $name) = @_;
  538.  
  539.   # Strip $(EXEEXT) from $name, so we can diagnose
  540.   # a clash if `ctags$(EXEEXT):' is redefined after `ctags:'.
  541.   (my $keyname = $name) =~ s,\$\(EXEEXT\)$,,;
  542.  
  543.   my $self = Automake::Item::new ($class, $name);
  544.   $_rule_dict{$keyname} = $self;
  545.   return $self;
  546. }
  547.  
  548.  
  549. =itcem C<@conds = define ($rulename, $source, $owner, $cond, $where)>
  550.  
  551. Define a new rule.  C<$rulename> is the list of targets.  C<$source>
  552. is the filename the rule comes from.  C<$owner> is the owner of the
  553. rule (C<RULE_AUTOMAKE> or C<RULE_USER>).  C<$cond> is the
  554. C<Automake::Condition> under which the rule is defined.  C<$where> is
  555. the C<Automake::Location> where the rule is defined.
  556.  
  557. Returns a (possibly empty) list of C<Automake::Condition>s where the
  558. rule's definition should be output.
  559.  
  560. =cut
  561.  
  562. sub define ($$$$$)
  563. {
  564.   my ($target, $source, $owner, $cond, $where) = @_;
  565.  
  566.   prog_error "$where is not a reference"
  567.     unless ref $where;
  568.   prog_error "$cond is not a reference"
  569.     unless ref $cond;
  570.  
  571.   # Don't even think about defining a rule in condition FALSE.
  572.   return () if $cond == FALSE;
  573.  
  574.   # For now `foo:' will override `foo$(EXEEXT):'.  This is temporary,
  575.   # though, so we emit a warning.
  576.   (my $noexe = $target) =~ s,\$\(EXEEXT\)$,,;
  577.   my $noexerule = rule $noexe;
  578.   my $tdef = $noexerule ? $noexerule->def ($cond) : undef;
  579.  
  580.   if ($noexe ne $target
  581.       && $tdef
  582.       && $noexerule->name ne $target)
  583.     {
  584.       # The no-exeext option enables this feature.
  585.       if (! option 'no-exeext')
  586.     {
  587.       msg ('obsolete', $tdef->location,
  588.            "deprecated feature: target `$noexe' overrides "
  589.            . "`$noexe\$(EXEEXT)'\n"
  590.            . "change your target to read `$noexe\$(EXEEXT)'");
  591.       msg ('obsolete', $where, "target `$target' was defined here");
  592.     }
  593.       # Don't `return ()' now, as this might hide target clashes
  594.       # detected below.
  595.     }
  596.  
  597.  
  598.   # A GNU make-style pattern rule has a single "%" in the target name.
  599.   msg ('portability', $where,
  600.        "`%'-style pattern rules are a GNU make extension")
  601.     if $target =~ /^[^%]*%[^%]*$/;
  602.  
  603.   # Diagnose target redefinitions.
  604.   if ($tdef)
  605.     {
  606.       my $oldowner  = $tdef->owner;
  607.       # Ok, it's the name target, but the name maybe different because
  608.       # `foo$(EXEEXT)' and `foo' have the same key in our table.
  609.       my $oldname = $tdef->name;
  610.  
  611.       # Don't mention true conditions in diagnostics.
  612.       my $condmsg =
  613.     $cond == TRUE ? '' : " in condition `" . $cond->human . "'";
  614.  
  615.       if ($owner == RULE_USER)
  616.     {
  617.       if ($oldowner == RULE_USER)
  618.         {
  619.           # Ignore `%'-style pattern rules.  We'd need the
  620.           # dependencies to detect duplicates, and they are
  621.           # already diagnosed as unportable by -Wportability.
  622.           if ($target !~ /^[^%]*%[^%]*$/)
  623.         {
  624.           ## FIXME: Presently we can't diagnose duplicate user rules
  625.           ## because we doesn't distinguish rules with commands
  626.           ## from rules that only add dependencies.  E.g.,
  627.           ##   .PHONY: foo
  628.           ##   .PHONY: bar
  629.           ## is legitimate. (This is phony.test.)
  630.  
  631.           # msg ('syntax', $where,
  632.           #      "redefinition of `$target'$condmsg...", partial => 1);
  633.           # msg_cond_rule ('syntax', $cond, $target,
  634.           #            "... `$target' previously defined here");
  635.         }
  636.           # Return so we don't redefine the rule in our tables,
  637.           # don't check for ambiguous condition, etc.  The rule
  638.           # will be output anyway beauce &read_am_file ignore the
  639.           # return code.
  640.           return ();
  641.         }
  642.       else
  643.         {
  644.           # Since we parse the user Makefile.am before reading
  645.           # the Automake fragments, this condition should never happen.
  646.           prog_error ("user target `$target'$condmsg seen after Automake's"
  647.               . " definition\nfrom " . $tdef->source);
  648.         }
  649.     }
  650.       else # $owner == RULE_AUTOMAKE
  651.     {
  652.       if ($oldowner == RULE_USER)
  653.         {
  654.           # -am targets listed in %dependencies support a -local
  655.           # variant.  If the user tries to override TARGET or
  656.           # TARGET-am for which there exists a -local variant,
  657.           # just tell the user to use it.
  658.           my $hint = 0;
  659.           my $noam = $target;
  660.           $noam =~ s/-am$//;
  661.           if (exists $dependencies{"$noam-am"})
  662.         {
  663.           $hint = "consider using $noam-local instead of $target";
  664.         }
  665.  
  666.           msg_cond_rule ('override', $cond, $target,
  667.                  "user target `$target' defined here"
  668.                  . "$condmsg...", partial => 1);
  669.           msg ('override', $where,
  670.            "... overrides Automake target `$oldname' defined here",
  671.            partial => $hint);
  672.           msg_cond_rule ('override', $cond, $target, $hint)
  673.         if $hint;
  674.  
  675.           # Don't overwrite the user definition of TARGET.
  676.           return ();
  677.         }
  678.       else # $oldowner == RULE_AUTOMAKE
  679.         {
  680.           # Automake should ignore redefinitions of its own
  681.           # rules if they came from the same file.  This makes
  682.           # it easier to process a Makefile fragment several times.
  683.           # Hower it's an error if the target is defined in many
  684.           # files.  E.g., the user might be using bin_PROGRAMS = ctags
  685.           # which clashes with our `ctags' rule.
  686.           # (It would be more accurate if we had a way to compare
  687.           # the *content* of both rules.  Then $targets_source would
  688.           # be useless.)
  689.           my $oldsource = $tdef->source;
  690.           return () if $source eq $oldsource && $target eq $oldname;
  691.  
  692.           msg ('syntax', $where, "redefinition of `$target'$condmsg...",
  693.            partial => 1);
  694.           msg_cond_rule ('syntax', $cond, $target,
  695.                  "... `$oldname' previously defined here");
  696.           return ();
  697.         }
  698.     }
  699.       # Never reached.
  700.       prog_error ("Unreachable place reached.");
  701.     }
  702.  
  703.   # Conditions for which the rule should be defined.
  704.   my @conds = $cond;
  705.  
  706.   # Check ambiguous conditional definitions.
  707.   my $rule = _crule $target;
  708.   my ($message, $ambig_cond) = $rule->conditions->ambiguous_p ($target, $cond);
  709.   if ($message)            # We have an ambiguity.
  710.     {
  711.       if ($owner == RULE_USER)
  712.     {
  713.       # For user rules, just diagnose the ambiguity.
  714.       msg 'syntax', $where, "$message ...", partial => 1;
  715.       msg_cond_rule ('syntax', $ambig_cond, $target,
  716.              "... `$target' previously defined here");
  717.       return ();
  718.     }
  719.       else
  720.     {
  721.       # FIXME: for Automake rules, we can't diagnose ambiguities yet.
  722.       # The point is that Automake doesn't propagate conditions
  723.       # everywhere.  For instance &handle_PROGRAMS doesn't care if
  724.       # bin_PROGRAMS was defined conditionally or not.
  725.       # On the following input
  726.       #   if COND1
  727.       #   foo:
  728.       #           ...
  729.       #   else
  730.       #   bin_PROGRAMS = foo
  731.       #   endif
  732.       # &handle_PROGRAMS will attempt to define a `foo:' rule
  733.       # in condition TRUE (which conflicts with COND1).  Fixing
  734.       # this in &handle_PROGRAMS and siblings seems hard: you'd
  735.       # have to explain &file_contents what to do with a
  736.       # condition.  So for now we do our best *here*.  If `foo:'
  737.       # was already defined in condition COND1 and we want to define
  738.       # it in condition TRUE, then define it only in condition !COND1.
  739.       # (See cond14.test and cond15.test for some test cases.)
  740.       @conds = $rule->not_always_defined_in_cond ($cond)->conds;
  741.  
  742.       # No conditions left to define the rule.
  743.       # Warn, because our workaround is meaningless in this case.
  744.       if (scalar @conds == 0)
  745.         {
  746.           msg 'syntax', $where, "$message ...", partial => 1;
  747.           msg_cond_rule ('syntax', $ambig_cond, $target,
  748.                  "... `$target' previously defined here");
  749.           return ();
  750.         }
  751.     }
  752.     }
  753.  
  754.   # Finally define this rule.
  755.   for my $c (@conds)
  756.     {
  757.       my $def = new Automake::RuleDef ($target, '', $where->clone,
  758.                        $owner, $source);
  759.       $rule->set ($c, $def);
  760.     }
  761.  
  762.   # We honor inference rules with multiple targets because many
  763.   # make support this and people use it.  However this is disallowed
  764.   # by POSIX.  We'll print a warning later.
  765.   my $target_count = 0;
  766.   my $inference_rule_count = 0;
  767.  
  768.   for my $t (split (' ', $target))
  769.     {
  770.       ++$target_count;
  771.       # Check if the rule is a suffix rule: either it's a rule for
  772.       # two known extensions...
  773.       if ($t =~ /^($KNOWN_EXTENSIONS_PATTERN)($KNOWN_EXTENSIONS_PATTERN)$/
  774.       # ...or it's a rule with unknown extensions (.i.e, the rule
  775.       # looks like `.foo.bar:' but `.foo' or `.bar' are not
  776.       # declared in SUFFIXES and are not known language
  777.       # extensions).  Automake will complete SUFFIXES from
  778.       # @suffixes automatically (see handle_footer).
  779.  
  780.  
  781.       || ($t =~ /$_SUFFIX_RULE_PATTERN/o && accept_extensions($1)))
  782.     {
  783.       ++$inference_rule_count;
  784.       register_suffix_rule ($where, $1, $2);
  785.     }
  786.     }
  787.  
  788.   # POSIX allows multiple targets before the colon, but disallows
  789.   # definitions of multiple inference rules.  It's also
  790.   # disallowed to mix plain targets with inference rules.
  791.   msg ('portability', $where,
  792.        "Inference rules can have only one target before the colon (POSIX).")
  793.     if $inference_rule_count > 0 && $target_count > 1;
  794.  
  795.   return @conds;
  796. }
  797.  
  798. =item C<depend ($target, @deps)>
  799.  
  800. Adds C<@deps> to the dependencies of target C<$target>.  This should
  801. be used only with factored targets (those appearing in
  802. C<%dependees>).
  803.  
  804. =cut
  805.  
  806. sub depend ($@)
  807. {
  808.   my ($category, @dependees) = @_;
  809.   push (@{$dependencies{$category}}, @dependees);
  810. }
  811.  
  812. =back
  813.  
  814. =head1 SEE ALSO
  815.  
  816. L<Automake::RuleDef>, L<Automake::Condition>,
  817. L<Automake::DisjConditions>, L<Automake::Location>.
  818.  
  819. =cut
  820.  
  821. 1;
  822.  
  823. ### Setup "GNU" style for perl-mode and cperl-mode.
  824. ## Local Variables:
  825. ## perl-indent-level: 2
  826. ## perl-continued-statement-offset: 2
  827. ## perl-continued-brace-offset: 0
  828. ## perl-brace-offset: 0
  829. ## perl-brace-imaginary-offset: 0
  830. ## perl-label-offset: -2
  831. ## cperl-indent-level: 2
  832. ## cperl-brace-offset: 0
  833. ## cperl-continued-brace-offset: 0
  834. ## cperl-label-offset: -2
  835. ## cperl-extra-newline-before-brace: t
  836. ## cperl-merge-trailing-else: nil
  837. ## cperl-continued-statement-offset: 2
  838. ## End:
  839.